package com.ruoyi.factory.ykj.service.impl;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.factory.ykj.service.InfoYkjService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.sql.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @ClassName InfoYkjServiceImpl
 * @Description: TODO
 * @Author LiuJiaji
 * @Date 2023/5/22 0022 9:56
 **/
@Service
public class InfoYkjServiceImpl implements InfoYkjService {
    protected final Logger logger = LoggerFactory.getLogger(InfoYkjServiceImpl.class);

    @Value("${epros.sendUrl}")
    private String sendEprosUrl;
    @Value("${erp.oracle.url}")
    private String oracleUrl;
    @Value("${erp.oracle.username}")
    private String oracleUserName;
    @Value("${erp.oracle.password}")
    private String oraclePassword;

    public void stafffee(){

            String sql = "";

            // 定义oracle数据库的驱动的类
            String driverClass = "oracle.jdbc.OracleDriver";
            // 定义连接的oracle的url
            String url = oracleUrl;
            // 用户名
            String username = oracleUserName;
            // 密码
            String password = oraclePassword;
            // 定义Connection连接
            Connection conn = null;
            // 定义数据库的sql执行对象
            Statement stmt = null;

            // 定义查询结果的ResultSet对象
            ResultSet rs = null;

            PreparedStatement pst = null;

            ResultSet rst = null;

            try {
                // 注册数据库的驱动程序
                Class.forName(driverClass);
                DriverManager.setLoginTimeout(60);
                // 获得数据库的连接
                conn = DriverManager.getConnection(url, username, password);
                // 创建sql执行对象
                stmt = conn.createStatement();
                sql ="SELECT bdc.pk_psndoc, bdc.code,bdc.name, wav.levelname FROM bd_psndoc  bdc left join hi_psndoc_wadoc hic on bdc.pk_psndoc =  hic.pk_psndoc AND hic.waflag='Y' AND hic.lastflag='Y' AND hic.pk_wa_item ='1001A110000000012VBZ' left join wa_prmlv wav on wav.pk_wa_prmlv= hic.pk_wa_prmlv";

                pst = conn.prepareStatement(sql);
                rst = pst.executeQuery();
                int i = 0;
                Map<String, String> map = new HashMap<>();
                    while (rst.next()) {
                    String code = rst.getString("CODE");
                    String levelname = rst.getString("levelname");
                    if(StringUtils.isNotEmpty(levelname)) map.put(code, levelname);
                }
                //处理修改
                String sql2 = "update hi_psnjob set JOBGLBDEF5 =? WHERE pk_psndoc  IN (SELECT pk_psndoc FROM bd_psndoc WHERE code=?)";
                Set<String> s1=map.keySet();
                //开始根据键找值
                int i2 =0;
                for (String key : s1) {
                    i2++;
//                    if(i2 == 290){
//                        if (pst != null) {
//                            pst.close();
//                        }
//                    }
                    String value=map.get(key);
                    pst = conn.prepareStatement(sql2);
                    pst.setString(1,value);
                    pst.setString(2,key);
                    int i1 = pst.executeUpdate();
                    if (pst != null) {
                        pst.close();
                    }
                    if(i1>0)logger.info("修改成功"+i2);

                }

//                pst = conn.prepareStatement(sql2);
//                pst.setString(1,"16");
//                pst.setString(2,"001182");
//                int i1 = pst.executeUpdate();
//                if(i1>0)logger.info("修改成功");

                if (pst != null) {
                    pst.close();
                }
                if (rst != null) {
                    rst.close();
                }
                if (rs != null) {
                    rs.close();
                }
                if (stmt != null) {
                    stmt.close();
                }
                if (conn != null) {
                    conn.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }


        }



}
